package com.dxiang.demo.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Jerry
 * @Date 2016年5月27日
 */
public class StrUtil {

	static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z','Z','X','C','V','B','N','M','L','K','J','H','G','F','D','S','A','Q','W','E','R','T','Y','U','I','P'};
    static final char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static final Random rand = new Random();
    
    static final String REGEX_EMAIL = "^[a-z0-9A-Z]+([-|_|\\.]?[a-z0-9A-Z]+)?@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
//    static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    static final String REGEX_PHONE = "^(13[0-9]|14[5|7]|15[^4,\\D]|18[0-9]|17[0|3|6|7|8])[0-9]{8}$";
    static final String REGEX_ACCOUNT = "^[a-zA-Z]\\w{5,17}$";
    static final String REGEX_PWD = "^[a-z0-9]{32}$";
    static final String REGEX_M5 = "^[a-z0-9]{32}$";
    static final String REGEX_TOKEN = "^[a-z0-9]{32}$";
    static final String REGEX_SQL = "[\"|'|\\-|\\|\\\\|\\+|\\?|\\*|#|<|>|=|%|!|\\^|\\$]";
    static final String REGEX_URL = "^\\w*$";

    /**
     * 邮箱验证
     * @param email
     * @return
     */
    public static boolean validEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return false;
        } else {
            Pattern pattern = Pattern.compile(REGEX_EMAIL);
            Matcher matcher = pattern.matcher(email);
            return matcher.matches();
        }
    }
    
    /**
     * 手机号校验
     * @param phoneNum
     * @return
     */
    public static boolean validPhone(String phoneNum){
    	if (StringUtils.isBlank(phoneNum))
		{
			return false;
		}
		Pattern pattern = Pattern.compile(REGEX_PHONE);
		Matcher matcher = pattern.matcher(phoneNum);
		return matcher.matches();
    }

    /**
     * 校验Token
     * @param token
     * @return 校验通过返回true，否则返回false
     */
    public static boolean validToken(String token) {
    	if (StringUtils.isBlank(token))
		{
			return false;
		}
    	return Pattern.matches(REGEX_TOKEN, token);
    }
    
    /**
	 * 校验用户名
	 * @param username
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean validAccount(String username) {
		if (StringUtils.isBlank(username))
		{
			return false;
		}
		return Pattern.matches(REGEX_ACCOUNT, username);
	}
	
	/**
	 * 校验密码
	 * @param password
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean validPwd(String password) {
		if (StringUtils.isBlank(password))
		{
			return false;
		}else{
			return Pattern.matches(REGEX_PWD, password);
		}
	}
	
	/**
	 * 校验MD5字符串
	 * @param m5Str
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean validM5Str(String m5Str) {
		if (StringUtils.isBlank(m5Str))
		{
			return false;
		}else{
			return Pattern.matches(REGEX_M5, m5Str);
		}
	}

    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    public static String randomString(int length) {
        StringBuffer sb = new StringBuffer();
        for (int loop = 0; loop < length; ++loop) {
            sb.append(hexDigits[rand.nextInt(hexDigits.length)]);
        }
        return sb.toString();
    }

    public static String randomNumber(int length) {
        StringBuffer sb = new StringBuffer();
        for (int loop = 0; loop < length; ++loop) {
            sb.append(digits[rand.nextInt(digits.length)]);
        }
        return sb.toString();
    }

    public static int str2int(String s) {
        if (s == null || s.equals("")) {
            return 0;
        } else {
            return Integer.parseInt(s);
        }
    }
    
    public static String noHtml(String s) {
        if (StringUtils.isBlank(s)) return "";
        else return s.replaceAll("<[.[^<]]*>", "");
    }

    public static String transHtml(String s) {
        if (StringUtils.isBlank(s)) return "";
        else return s.replace("<", "&lt;").replace(">", "&gt;");
    }

    public static String transSql(String s){
    	if (StringUtils.isBlank(s))
		{
			return "";
		}
    	return s.replaceAll(REGEX_SQL, "");
    }
    
    public static boolean areNotEmpty(String[] values)
    {
      boolean result = true;
      if ((values == null) || (values.length == 0))
        result = false;
      else {
        for (String value : values) {
          result &= !StringUtils.isEmpty(value);
        }
      }
      return result;
    }
    
    public static boolean isNumeric(Object obj)
    {
      if (obj == null) {
        return false;
      }
      char[] chars = obj.toString().toCharArray();
      int length = chars.length;
      if (length < 1) {
        return false;
      }
      int i = 0;
      if ((length > 1) && (chars[0] == '-'));
      for (i = 1; 
        i < length; i++) {
        if (!Character.isDigit(chars[i])) {
          return false;
        }
      }
      return true;
    }
    
}
